package com.zhihuichongcheng.application.ui.task.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.bean.task.FilterBean;
import com.zhihuichongcheng.application.bean.task.TaskBean;
import com.zhihuichongcheng.application.bean.task.TaskPriority;
import com.zhihuichongcheng.application.bean.task.TaskType;
import com.zhihuichongcheng.application.ui.task.adapter.holder.FilterDataHolder;
import com.zhihuichongcheng.application.ui.task.adapter.holder.FilterHeadHolder;
import com.zhihuichongcheng.application.ui.task.adapter.holder.FilterHolder;
import com.zhihuichongcheng.application.util.CollectionUtils;
import com.zhihuichongcheng.application.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LK on 2017/1/6.
 */
public class FilterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final int TYPE_HEADER = 0;
    private final int TYPE_FILTER = 1;
    private final int TYPE_DATA = 2;

    private List<FilterBean> dataList = new ArrayList<>();

    private FilterDataHolder dateHolder;
    //默认数据
    private List<String> defaultUserIds = new ArrayList<>();
    private int defaultTaskType = -1;
    private int defaultTaskLv = -1;
    private String defaultFinishTime;

    public void setUserLists(final List<UserBean> userList) {
        //任务类型
        setTypeFilters();
        //参与者
        setUserFilter(userList);
        //优先级
        setLvFilter();
    }

    /**
     * 设置类型
     */
    private void setTypeFilters() {
        List<TaskType> taskTypes = TaskBean.taskTypeList;
        if (!CollectionUtils.isEmpty(taskTypes)) {
            FilterBean itemTypeTag = new FilterBean("任务类型", 0);
            itemTypeTag.setType(FilterBean.FilterType.TAG);
            dataList.add(itemTypeTag);
            CollectionUtils.iterator(taskTypes, new CollectionUtils.AIIteratorListCallBack() {
                @Override
                public boolean callback(Object data, int position) {
                    TaskType taskType = (TaskType) data;
                    FilterBean itemTypeFilter = new FilterBean("任务类型", taskType.getId());
                    itemTypeFilter.setName(taskType.getName());
                    if (defaultTaskType == taskType.getId())
                        itemTypeFilter.setSelected(true);
                    dataList.add(itemTypeFilter);
                    return false;
                }
            });
        }
    }

    /**
     * 设置相关用户
     */
    public void setUserFilter(List<UserBean> userList) {
        if (CollectionUtils.isEmpty(userList)) return;
        final List<FilterBean> users = new ArrayList<>();
        FilterBean filterBean_2_0 = new FilterBean("参与者", 0);
        filterBean_2_0.setType(FilterBean.FilterType.TAG);
        users.add(filterBean_2_0);
        CollectionUtils.iterator(userList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean userBean = (UserBean) data;
                FilterBean itemFilter = new FilterBean("参与者", userBean.getUserId());
                itemFilter.setName(userBean.getUserName());
                itemFilter.setType(FilterBean.FilterType.Multiple);
                if (defaultUserIds.contains(itemFilter.getValue() + ""))
                    itemFilter.setSelected(true);
                users.add(itemFilter);
                return false;
            }
        });
        dataList.addAll(users);
    }

    /**
     * 设置优先级
     */
    private void setLvFilter() {
        List<TaskPriority> taskLv = TaskBean.taskPriorities;
        if (CollectionUtils.isEmpty(taskLv)) return;

        FilterBean itemLvTag = new FilterBean("优先级", 0);
        itemLvTag.setType(FilterBean.FilterType.TAG);
        dataList.add(itemLvTag);
        CollectionUtils.iterator(taskLv, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                TaskPriority itemPriority = (TaskPriority) data;
                FilterBean filterBean = new FilterBean("优先级", itemPriority.getId());
                filterBean.setName(itemPriority.getName());
                if (defaultTaskLv == itemPriority.getId())
                    filterBean.setSelected(true);
                dataList.add(filterBean);
                return false;
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view;
        switch (viewType) {
            case TYPE_HEADER:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_filter_head, parent, false);
                holder = new FilterHeadHolder(view);
                break;
            case TYPE_FILTER:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_filter, parent, false);
                holder = new FilterHolder(view);
                break;
            case TYPE_DATA:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_filter_data, parent, false);
                dateHolder = new FilterDataHolder(view);
                return dateHolder;
            default:
                return null;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FilterHeadHolder) {
            ((FilterHeadHolder) holder).setData(dataList.get(position));
        } else if (holder instanceof FilterHolder) {
            ((FilterHolder) holder).setData(dataList.get(position), this, position);
        } else if (holder instanceof FilterDataHolder) {
            ((FilterDataHolder) holder).setData(defaultFinishTime);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == -1) {
            return super.getItemViewType(position);
        }
        if (position != getItemCount() - 1) {
            if (dataList.get(position).getType() == FilterBean.FilterType.TAG) {
                return TYPE_HEADER;
            }
            return TYPE_FILTER;
        } else {
            return TYPE_DATA;
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    /**
     * 更新单选按钮选中状态
     */
    public void notifySingleItemSelected(final FilterBean filterBean) {
        filterBean.setSelected(!filterBean.isSelected());
        CollectionUtils.iterator(dataList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                FilterBean itemData = (FilterBean) data;
                if (filterBean.getTag().equals(itemData.getTag())) {
                    if (!StringUtils.isEmpty(itemData.getName()) &&
                            !itemData.getName().equals(filterBean.getName())) {
                        itemData.setSelected(false);
                    }
                }
                return false;
            }
        });
    }

    public String getSelectedUserIds() {
        final StringBuilder result = new StringBuilder();
        CollectionUtils.iterator(dataList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                FilterBean itemData = (FilterBean) data;
                if (itemData.isSelected() && itemData.getTag().equals("参与者")) {
                    result.append(itemData.getValue() + ",");
                }
                return false;
            }
        });
        String resultStr = result.toString().trim();
        if (result.length() > 0) {
            resultStr = resultStr.substring(0, resultStr.length() - 1);
        }
        return resultStr;
    }

    public void setSelectedUserIds(String userId) {
        defaultUserIds.clear();
        if (StringUtils.isEmpty(userId))
            return;
        String[] userIds = userId.split(",");
        CollectionUtils.iterator(userIds, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                if (null != data && !StringUtils.isEmpty(data.toString())) {
                    defaultUserIds.add(data.toString().trim());
                }
                return false;
            }
        });

    }

    public int getSelectedTaskType() {
        final int[] result = {-1};
        CollectionUtils.iterator(dataList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                FilterBean itemData = (FilterBean) data;
                if (itemData.isSelected() && itemData.getTag().equals("任务类型")) {
                    result[0] = Integer.parseInt(itemData.getValue().toString());
                    return true;
                }
                return false;
            }
        });
        return result[0];

    }

    public int getSelectedPriority() {
        final int[] result = {-1};
        CollectionUtils.iterator(dataList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                FilterBean itemData = (FilterBean) data;
                if (itemData.isSelected() && itemData.getTag().equals("优先级")) {
                    result[0] = Integer.parseInt(itemData.getValue().toString());
                    return true;
                }
                return false;
            }
        });
        return result[0];

    }

    public String getSelectedDate() {
        if (null == dateHolder)
            return "";
        else
            return dateHolder.getData();
    }

    public void setTaskType(int taskType) {
        this.defaultTaskType = taskType;
    }

    public void setTaskLv(int taskLv) {
        this.defaultTaskLv = taskLv;
    }

    public void setFinishTime(String finishTime) {
        this.defaultFinishTime = finishTime;
    }
}
